//
//  MyConcernNoticesDetailViewController.m
//  homeland
//
//  Created by SunShine on 13-10-20.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MyConcernNoticesDetailViewController.h"
#import "NoticesItem.h"
#import "MyConcernFileViewController.h"
#import "UIColor+expanded.h"
@interface MyConcernNoticesDetailViewController ()
@property (strong, nonatomic) NSString *fileUrl;
@end

@implementation MyConcernNoticesDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    [self configView];
}

-(void)configView
{
    NoticesItem *item = [_config objectForKey:@"dataItem"];
    //根据内容调整高度
    CGFloat totalHeight;
    CGFloat titleHeight = [self caculateHeightForString:item.title AndFontSize:18.0f AndWitdh:300.0f];
    CGFloat contentHeight = [self caculateHeightForString:item.detail AndFontSize:18.0f AndWitdh:300.0f];
    NSLog(@"计算出标题高度%f正文高度%f", titleHeight, contentHeight);
    self.titleTextView.text = item.title;
    self.titleTextView.textColor=[UIColor colorWithHexString:@"00bfff"];
    self.titleTextView.frame = CGRectMake(10.0, 10.0, 300.0, titleHeight);
    self.contentTextView.text = item.detail;
    self.contentTextView.frame = CGRectMake(10.0, CGRectGetMaxY(self.titleTextView.frame) + 10.0, 300.0, contentHeight);
//    self.contentTextView.backgroundColor = [UIColor grayColor];
    self.fileUrl = item.fileUrl;
    if (_fileUrl && _fileUrl != (id)[NSNull null] && _fileUrl.length>0) {
        [self.fileDownloadButton setTitle:@"点击下载并打开附件" forState:UIControlStateNormal];
        self.fileDownloadButton.frame = CGRectMake(10.0, CGRectGetMaxY(self.contentTextView.frame) + 10.0, 300.0, 44.0);
        totalHeight = MAX(CGRectGetMaxY(self.fileDownloadButton.frame) + 10.0, CGRectGetHeight(self.scrollView.frame));
    } else {
        self.fileDownloadButton.hidden = YES;
        totalHeight = MAX(CGRectGetMaxY(self.contentTextView.frame) + 10.0, CGRectGetHeight(self.scrollView.frame));
    }
    self.scrollView.contentSize = CGSizeMake(320.0f, totalHeight);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)fileButtonAction:(id)sender
{
    if (self.fileUrl && self.fileUrl.length >0) {
        NSLog(@"准备下载文件%@", _fileUrl);
        MyConcernFileViewController *fileVC = [[MyConcernFileViewController alloc]initWithNibName:@"MyConcernFileViewController" bundle:nil];
        fileVC.config = [NSDictionary dictionaryWithObjectsAndKeys:_fileUrl, @"fileUrl", nil];
        [self.navigationController pushViewController:fileVC animated:YES];
    }
}
@end
